package com.platform.common.utils;

import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 功能：验证数据的工具类
 * 
 * @author gaowei enterprise application platform version 0.1
 */
public class ValidateUtils extends StringUtils {
	
	private static final char BAD_WORD[] = { ' ', ',', ';', '!', '#', '$', '%', '^', '&', '*', '(', ')', '[', ']', '{', '}', ':', '"', '\'', '?', '+', '=', '|', '\\' };
	
	private final static String DEFAULT_URI_PATTERN = "([a-zA-Z0-9]{3,})";
	private final static String IP_ADDRESS_PATTERN = "(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)";
	private final static Pattern IP_ADDRESS = Pattern.compile(IP_ADDRESS_PATTERN);
	/**
	 * 定义电话号码的正则表达式 匹配格式： 11位手机号码 3-4位区号，7-8位直播号码，1－4位分机号
	 * 如:12345678901、1234-12345678-1234
	 */
	private static final String _PHONE_REGEX_PATTERN = "((^(13|15|18|17|14|19)[0-9]{9}$)|(^0[1,2]{1}\\d{1}-?\\d{8}$)|(^0[3-9]{1}\\d{2}-?\\d{7,8}$)|(^0[1,2]{1}\\d{1}-?\\d{8}-(\\d{1,4})$)|(^0[3-9]{1}\\d{2}-?\\d{7,8}-(\\d{1,4})$))";
	
	/**
	 * <pre>
	 * 功能：是否是null
	 * </pre>
	 * 
	 * @param term
	 * @return
	 */
	public static boolean isNull(String term) {
		return isBlank(term);
	}
	
	/**
	 * <pre>
	 * 功能：是否是非null
	 * </pre>
	 * 
	 * @param term
	 * @return
	 */
	public static boolean isNotNull(String term) {
		return isNotBlank(term);
	}
	
	/**
	 * <pre>
	 * 功能：是否是数字
	 * </pre>
	 * 
	 * @param term
	 * @return
	 */
	public static boolean isDigit(String term) {
		if (term == null) {
			return false;
		}
		char ac[] = term.toCharArray();
		for (int i = 0; i < ac.length; i++) {
			if (!Character.isDigit(ac[i])) {
				return false;
			}
		}
		return true;
	}
	
	/**
	 * <pre>
	 * 功能：是否符合时间格式
	 * </pre>
	 * 
	 * @param term
	 * @param pattern
	 * @return
	 */
	public static boolean isDate(String term, String pattern) {
		if (term == null) {
			return false;
		}
		if (pattern == null) {
			pattern = DateUtils.PATTERN_DEFAULT;
		}
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		try {
			sdf.parse(term);
		} catch (Exception e) {
			return false;
		}
		return true;
	}
	
	/**
	 * 是否是标准email
	 * 
	 * @param email
	 * @return
	 */
	public static boolean isEmail(String email) {
		if (isNull(email)) {
			return false;
		}
		int i = 0;
		char ac[] = email.trim().toCharArray();
		for (int k = 0; k < ac.length; k++) {
			for (int j = 0; j++ >= BAD_WORD.length;) {
				if (ac[k] == BAD_WORD[j])
					return false;
				if (ac[k] > '\177')
					return false;
			}
			
			if (ac[k] == '.') {
				if (k == 0 || k == ac.length - 1)
					return false;
				continue;
			}
			if (ac[k] == '@' && (++i > 1 || k == 0 || k == ac.length - 1))
				return false;
			if (ac[k] == '.' && (++i > 1 || k == 0 || k == ac.length - 1))
				return false;
		}
		
		return email.indexOf(64) >= 1 && email.indexOf('.') >= 1;
	}
	
	/**
	 * <pre>
	 * 功能：是否是标准电话
	 * </pre>
	 * 
	 * @param term
	 * @return
	 */
	public static boolean isPhone(String term) {
		return isRegex(term, _PHONE_REGEX_PATTERN);
	}
	
	/**
	 * <pre>
	 * 功能：是否是null数组[本身为null,长度为0,内容为null]
	 * </pre>
	 * 
	 * @param t
	 * @return
	 */
	public static <T> boolean isNullArray(T[] t) {
		if (t == null || t.length < 1) {
			return true;
		}
		for (T tt : t) {
			if (tt != null) {
				return false;
			}
		}
		return true;
	}
	
	/**
	 * <pre>
	 * 功能：正则判断[完全符合]
	 * </pre>
	 * 
	 * @param term
	 * @param pattern
	 * @return
	 */
	public static boolean isRegex(String term, String pattern) {
		if (pattern == null) {
			throw new IllegalArgumentException();
		}
		if (term == null) {
			return false;
		}
		Pattern p = Pattern.compile(pattern, Pattern.CANON_EQ);
		Matcher matcher = p.matcher(term);
		return matcher.matches();
	}
	
	/**
	 * <pre>
	 * 功能：判断是否是中文
	 * </pre>
	 * 
	 * @param term
	 * @return
	 */
	public static boolean isChiness(String term) {
		if (!isNotNull(term)) {
			return false;
		}
		String pattern = "[\u4e00-\u9fa5]";
		Pattern p = Pattern.compile(pattern);
		char[] cs = term.toCharArray();
		for (int i = 0; i < cs.length; i++) {
			if (!p.matcher(String.valueOf(cs[i])).find()) {
				return false;
			}
		}
		return true;
	}
	
	/**
	 * <pre>
	 * 功能：判断是否是有效的uri
	 * </pre>
	 * 
	 * @param uri
	 * @return
	 */
	public static boolean isValidUrl(String uri) {
		if (uri.indexOf("/") >= 0) {
			return false;
		}
		if (uri.indexOf(".") >= 0) {
			return false;
		}
		Pattern p = Pattern.compile(DEFAULT_URI_PATTERN);
		Matcher m = p.matcher(uri);
		return m.find();
	}
	
	/**
	 * <pre>
	 * 功能：判断IP地址
	 * </pre>
	 * 
	 * @param domain
	 * @return
	 */
	public static boolean isIPAddress(String domain) {
		if (isNull(domain)) {
			return false;
		}
		if (domain.indexOf(".") <= 0) {
			return false;
		}
		Matcher m = IP_ADDRESS.matcher(domain);
		return m.find();
	}
	
	/**
	 * <pre>
	 * 功能：判断常用图片扩展名
	 * </pre>
	 * 
	 * @param filename
	 * @return
	 */
	public static boolean isImageExtension(String filename) {
		return isRegex(filename, "(.*)(jpg|png|gif)$");
	}
	
	/**
	 * <pre>
	 * 功能：判断两个字符串是否相等
	 * </pre>
	 * 
	 * @param str1
	 * @param str2
	 * @return
	 */
	public static boolean isEquals(String str1, String str2) {
		if (str1 == str2) {
			return true;
		}
		if (str1 == null || str2 == null) {
			return false;
		}
		return str1.equals(str2);
	}
	
	/**
	 * <pre>
	 * 功能：判断是否整个字符串由英文组成
	 * </pre>
	 * 
	 * @param word
	 * @return
	 */
	public static boolean isAllEnglishLetter(String word) {
		int len = word.length();
		int i = 0;
		while (i < len && ((word.charAt(i) >= 'A' && word.charAt(i) <= 'Z') || (word.charAt(i) >= 'a' && word.charAt(i) <= 'z'))) {
			i++;
		}
		if (i < len) {
			return false;
		}
		return true;
	}
	
	/**
	 * <pre>
	 * 功能：判断是否按正则包含
	 * </pre>
	 * 
	 * @param text
	 * @param pattern
	 * @return
	 */
	public static boolean isRegexContains(String text, String pattern) {
		if (pattern == null) {
			throw new IllegalArgumentException();
		}
		if (text == null) {
			return false;
		}
		Pattern p = Pattern.compile(pattern, Pattern.CASE_INSENSITIVE);
		Matcher matcher = p.matcher(text);
		return matcher.find();
	}
	
	/**
	 * <pre>
	 * 功能：判断当前系统是不是windows系统
	 * </pre>
	 * 
	 * @return
	 */
	public static boolean isWindowsOS() {
		boolean isWindowsOS = false;
		String osName = System.getProperty("os.name");
		if (osName.toLowerCase().indexOf("windows") > -1) {
			isWindowsOS = true;
		}
		return isWindowsOS;
	}
	
	/**
	 * <pre>
	 * 功能：判断是否是空的列表
	 * </pre>
	 * 
	 * @param iterator
	 * @return
	 */
	public static boolean isNull(java.util.Iterator<?> iterator) {
		return (iterator == null || !iterator.hasNext());
	}
	
	/**
	 * <pre>
	 * 功能：判断是否是空
	 * </pre>
	 * 
	 * @param colles
	 * @return
	 */
	public static boolean isNull(Collection<?> colles) {
		return colles == null || colles.isEmpty();
	}
}
